package com.mdhw.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdhw.admin.dao.AdminLogDao;
import com.mdhw.admin.entity.AdminLog;
import com.mdhw.admin.entity.AdminUser;
import com.mdhw.admin.service.AdminLogService;
import com.mdhw.admin.service.AdminUserService;
import com.mdhw.admin.vo.AdminLogVo;
import com.mdhw.admin.vo.AdminShangwuRegionTree;
import com.mdhw.admin.vo.AdminUserVo;
import com.mdhw.common.base.PageData;
import com.mdhw.common.enums.StatusEnum;
import com.mdhw.config.R;
import com.mdhw.system.dao.SysRegionDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【admin_log(操作日志)】的数据库操作Service实现
* @createDate 2024-12-04 13:47:43
*/
@Service
public class AdminLogServiceImpl extends ServiceImpl<AdminLogDao, AdminLog> implements AdminLogService{

    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private AdminLogDao adminLogDao;
    @Autowired
    private SysRegionDao sysRegionDao;
    @Override
    public R<PageData<AdminLogVo>> selectAdminLogPage(Long pageNo, Long pageSize, String startTime, String endTime, Long userId, Integer sysType,
                                                      String moduleName, String moduleValue,String paramsKeyWord,Integer responseSuccess) {
        AdminUserVo adminUserVo = this.adminUserService.selectAdminUserByToken();
        if(adminUserVo == null){
            return R.success("未登录，无权限！");
        }
        Boolean isAdmin = adminUserVo.getIsAdmin();
        if(!isAdmin){
            return R.success("非超级管理员，无权限！");
        }
        Page page = new Page(pageNo,pageSize);
        QueryWrapper<AdminLogVo> alvLqw = new QueryWrapper<>();
        if(StringUtils.isNotBlank(startTime)){
            alvLqw.ge("create_date",startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            alvLqw.le("create_date",endTime);
        }
        if(userId != null){
            alvLqw.eq("create_user",userId);
        }
        if(sysType != null && sysType != 0){
            alvLqw.eq("sys_type",sysType);
        }
        if(StringUtils.isNotBlank(moduleName)){
            alvLqw.eq("module_name",moduleName);
        }
        if(StringUtils.isNotBlank(moduleValue)){
            alvLqw.eq("module_value",moduleValue);
        }
        if(StringUtils.isNotBlank(paramsKeyWord)){
            alvLqw.like("request","%"+paramsKeyWord+"%");
        }
        if(responseSuccess != null){
            alvLqw.eq("response_success",responseSuccess);
        }
        alvLqw.orderByDesc("id");
        this.adminLogDao.selectAdminLogPage(page,alvLqw);
        PageData<AdminLogVo> pageData = PageData.plusPageToPageData(page);
        List<AdminLogVo> dataList = pageData.getData();
        List<Long> createUserIdList  = dataList.stream().map(AdminLogVo::getCreateUser).collect(Collectors.toList());
        createUserIdList.add(-11L);
        LambdaQueryWrapper<AdminUser> auLqw = new LambdaQueryWrapper<>();
        auLqw.in(AdminUser::getId,createUserIdList);
        List<AdminUser> adminUserList = this.adminUserService.list(auLqw);
        Map<Long, AdminUser> adminUserMap = adminUserList.stream().collect(Collectors.toMap(AdminUser::getId, d -> d, (o, n) -> n));

        for(AdminLogVo adminLogVo:dataList){
            Integer responseSucess = adminLogVo.getResponseSucess();
            if(responseSucess == 0){
                adminLogVo.setResponseSuccessValue("失败");
            }else if(responseSucess == 1){
                adminLogVo.setResponseSuccessValue("成功");
            }

            Integer sysType1 = adminLogVo.getSysType();
            if(sysType1 == 1){
                adminLogVo.setSysTypeValue("后台系统");
            }else if(sysType1 == 2){
                adminLogVo.setSysTypeValue("美宅商家");
            }else if(sysType1 == 3){
                adminLogVo.setSysTypeValue("美宅聚宝");
            }
            Long createUser = adminLogVo.getCreateUser();
            AdminUser adminUser = adminUserMap.get(createUser);
            if(adminUser != null){
                String name = adminUser.getName();
                if(StringUtils.isBlank(name)){
                    name = adminUser.getLoginName();
                }
                adminLogVo.setCreateUserValue(name);
            }else{
                if(createUser != null){
                    adminLogVo.setCreateUserValue(createUser.toString());
                }
            }
            String requestProvince = adminLogVo.getRequestProvince();
            String requestCity = adminLogVo.getRequestCity();
            String requestArea = adminLogVo.getRequestArea();
            String requestAddress = "";
            if(StringUtils.isNotBlank(requestProvince)){
                requestAddress = requestAddress+requestProvince;
            }
            if(StringUtils.isNotBlank(requestCity)){
                requestAddress = requestAddress+requestCity;
            }
            if(StringUtils.isNotBlank(requestArea)){
                requestAddress = requestAddress+requestArea;
            }
            adminLogVo.setRequestAddress(requestAddress);
        }
        return R.success(pageData);
    }



    private List<AdminShangwuRegionTree> getChild(Long parentId, List<AdminShangwuRegionTree> sysRegionTreeList){
        // 子菜单列表
        List<AdminShangwuRegionTree> childList = new ArrayList<>();
        for(AdminShangwuRegionTree sysMenuTree:sysRegionTreeList){
            if(sysMenuTree.getPid().toString().equals(parentId.toString())){
                childList.add(sysMenuTree);
            }
        }

        // 遍历 获取子菜单的子菜单
        for (AdminShangwuRegionTree menu : childList) {
            List<AdminShangwuRegionTree> child = getChild(menu.getId(), sysRegionTreeList);
            if(child != null && child.size() > 0){
                menu.setChildren(child);
            }
        }
        //递归出口 childList长度为0
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

}




